import com.sun.org.apache.xpath.internal.functions.FuncQname;
import lombok.SneakyThrows;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;

public class ChatChannel implements Runnable {
    private static final HashMap<String, ObjectOutputStream> allMap = new HashMap<>();
    private Socket socket;
    private ObjectInputStream ois;
    private ObjectOutputStream oos;
    private String name;
    private Boolean flag = true;

    public ChatChannel(Socket socket) {
        this.socket = socket;
    }

    @SneakyThrows
    @Override
    public void run() {
        try {
            while (true) {
                if (flag) {
                    ois = new ObjectInputStream(socket.getInputStream());
                    oos = new ObjectOutputStream(socket.getOutputStream());
                    name = (String) ois.readObject();
//                收集通道到hashmap中，用于消息群发
                    allMap.put(name,oos);
                    System.out.println(name + "进入聊天室！");
//                向客户端发送欢迎信息
                    oos.writeObject(name + ",您好！欢迎您进入聊天室");
                    oos.flush();
//                调用群聊通知
                    sendToAll("系统消息： " + name + "进入聊天室！");
                    flag = false;
                }

                String msg = (String) ois.readObject();
                System.out.println(name + "：" + msg);

//            System.out.println(msg.startsWith("@"));
//            判断是否私聊
                if(msg.startsWith("@")&&msg.indexOf("#")>0) {
//                调用私聊方法
                    secreSend(name,msg);
                } else {
                    //            群聊
                    for (ObjectOutputStream oos: allMap.values()) {
                        oos.writeObject(name + "：" + msg);
                        oos.flush();
                    }
                }
            }
        } catch (IOException e) {
//            e.printStackTrace();
            System.out.println(name + "失联啦！");
            try {
                socket.close();
                allMap.remove(name);
                sendToAll(name + "失联啦！");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    //    封装私聊方法
    private void secreSend(String name,String msg) throws IOException {
        int endIndex = msg.indexOf("#");
//        目的客户端
//        String secreName = msg.split("@")[1].split(":")[0];
        String secreName = msg.substring(1,endIndex);
//        发送内容
//        String secreMsg = msg.split(":")[1];
        String secreMsg = msg.substring(endIndex+1);
//        secreOos指定私聊目的客户端
        ObjectOutputStream secreOos = allMap.get(secreName);
        secreOos.writeObject(name + "在你耳边说：" + secreMsg);
        secreOos.flush();
    }

    //    有其他人进入聊天室时，发送系统消息
    private void sendToAll(String msg) throws IOException {
        for (ObjectOutputStream oos: allMap.values()) {
            oos.writeObject(msg);
            oos.flush();
        }
    }
}
